#include("../../layout/header.html")
#@layout()
#define content()
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格搜索栏 -->
            <form id="form" class="layui-form toolbar">
                <div id="demo" class="layui-form-item">

                    <div class="layui-inline">
                        <label class="layui-form-label">区划名称:</label>
                        <div class="layui-input-inline">
                            <input name="name" class="layui-input"
                                   placeholder="请输入区划名称"/>
                        </div>
                    </div>


                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="dataTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</div>
<!-- 表单弹窗 -->
<script type="text/html" id="dataEditDialog">
    <form id="dataEditForm" lay-filter="dataEditForm" class="layui-form model-form">
        <input name="id" type="hidden"/>

        <div class="layui-form-item">
            <label class="layui-form-label">区划等级:</label>
            <div id="levelRadio" class="layui-input-block"></div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">区划名称:</label>
            <div class="layui-input-block">
                <input name="name" class="layui-input" placeholder="请输入区划名称"/>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">经度:</label>
            <div class="layui-input-block">
                <input name="longitude" type="number" class="layui-input" placeholder="请输入经度"/>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">纬度:</label>
            <div class="layui-input-block">
                <input name="latitude" type="number" class="layui-input" placeholder="请输入纬度"/>
            </div>
        </div>


        <div class="layui-form-item text-right ai-form-footer">
            <button class="layui-btn  layui-hide" lay-filter="dataEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary layui-hide" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="dataTbBar">
    <a id="areaEdit" class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-search"></i>查看</a>
    <!--    <a id="areaDel" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
</script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'formX', 'laydate', 'treeTable'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var formX = layui.formX;
        var laydate = layui.laydate;
        var admin = layui.admin;
        var treeTable = layui.treeTable;
        var selObj, treeData, insTb;
        var areaLevel = [];

        treeRender();
        admin.getDict('areaLevel,sex', function (d) {
            areaLevel = d.data.areaLevel
        });

        function treeRender() {
            var loadIndex = layer.load(2);
            admin.req('/sys/area/list', {page: 1, limit: 10000}, function (d) {
                if (d.code === 0) {
                    insTb = treeTable.render({
                        elem: '#dataTable',
                        data: d.data,
                        tree: {
                            iconIndex: 0,           // 折叠图标显示在第几列
                            isPidData: true,        // 是否是id、pid形式数据
                            idName: 'id',  // id字段名称
                            pidName: 'parentId'     // pid字段名称
                        },
                        height: 'full-150',
                        cols: [[
                            {field: 'name', title: '区划名称'},
                            {field: 'levelStr', title: '区划等级'},
                            {field: 'id', title: 'ID/code值'},
                            {field: 'longitude', title: '经度'},
                            {field: 'latitude', title: '纬度'},
                            {title: '操作', toolbar: '#dataTbBar', align: 'center', width: 200, minWidth: 200}
                        ]]
                        , done: function (d) {
                            // insTb.expandAll();
                            treeData = d;
                            layer.close(loadIndex);
                        }
                    });
                    /* 表格工具条点击事件 */
                    treeTable.on('tool(dataTable)', function (obj) {
                        if (obj.event === 'edit') { // 修改
                            console.log(obj)
                            showEditModel(obj.data);
                        } else if (obj.event === 'del') { // 删除
                            doDel(obj);
                        }
                    });

                    /* 表格搜索 */
                    form.on('submit(dataTbSearch)', function (data) {
                        doTbSearch(data.field.name);

                        return false;
                    });

                    /* 修改 */
                    $('#areaEdit').click(function () {
                        showEditModel(selObj.data);
                    });

                    /* 删除 */
                    $('#areaDel').click(function () {
                        doDel(selObj);
                    });

                    /* 搜索表格数据 */
                    function doTbSearch(field) {
                        if (field) {
                            insTb.filterData(field);
                        } else {
                            insTb.clearFilter();
                        }

                    }
                }
            })
        }


        /* 表格搜索 */
        form.on('submit(dataTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            console.log(mData)
            admin.popupRight({
                type: 1,
                title: (mData ? '修改' : '添加') + '区域',
                content: $('#dataEditDialog').html(),
                success: function (layero, dIndex) {
                    formX.renderRadio({
                        elem: '#levelRadio',
                        data: areaLevel,
                        name: 'level',
                        title: 'name',
                        value: 'value',
                    })


                    // 回显表单数据
                    form.val('dataEditForm', mData);

                    // 表单提交事件
                    form.on('submit(dataEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);


                        admin.post('/sys/area/add', data.field, function (res) {
                            layer.close(loadIndex);
                            if (0 === res.code) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({page: {curr: 1}});
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        });
                        return false;
                    });
                },
                end: function () {

                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                admin.req('/sys/area/del', {
                    id: obj.data ? obj.data.id : '',
                    ids: obj.ids ? obj.ids.join(',') : ''
                }, function (res) {
                    layer.close(loadIndex);
                    if (0 === res.code) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                });
            });
        }

    });
</script>
</body>
#end
